# -*- codeing = utf-8 -*-
# @Time : 2023/12/27 0027 下午 22:31
# @Author : baiguanghui
# @File : baseCase.py
# @Software : PyCharm

from django.shortcuts import render
from django.http import JsonResponse
from django.core import serializers

from analysis import models
from analysis.forms import ananlycase
from utils.pagination import Pagination


def basecase(request):
    objs = models.BaseData.objects.all()
    compare_objs = models.CompareData.objects.all()

    page_object = Pagination(
        current_page=request.GET.get('page'),
        all_count=objs.count(),
        base_url=request.path_info,
        query_params=request.GET,
        per_page=15
    )
    issues_object_list = objs[page_object.start:page_object.end]
    content = {
        'objs': objs,
        'compare_objs': compare_objs,
        'issues_object_list': issues_object_list,
        'page_html': page_object.page_html()
    }

    return render(request, 'analy/basecase.html', content)


def add_sing_case(request):
    if request.method == 'POST':
        print(request.POST)
        cmd = request.POST.get('cmd')
        comment = request.POST.get('comment')
        if not cmd:
            content = {
                "status": False,
                "message": "用例不能为空"
            }
            return JsonResponse(content)
        is_exist = models.BaseData.objects.filter(cmd=cmd).exists()
        if is_exist:
            content = {
                "status": False,
                "message": "该用例已存在"
            }
        else:
            instance = models.BaseData.objects.create(cmd=cmd, comment=comment)
            content = {
                "status": True,
                "message": "success",
                "url": "/bigsdp/analy/basecase/"
            }
        return JsonResponse(content)


def del_sing_case(request):
    if request.method == "POST":
        delCaseId = request.POST.get('delCaseId')
        obj = models.BaseData.objects.filter(id=delCaseId).first().delete()
        content = {
            'obj': obj
        }

        return JsonResponse(content)


def audit_cases(request):
    if request.method == "POST":
        # print(request.POST.get('csrfmiddlewaretoken'))
        all_case = models.BaseData.objects.all()
        for item in all_case:
            id = item.id
            first_obj = models.CompareData.objects.filter(cmd_id=id).first()
            last_obj = models.CompareData.objects.filter(cmd_id=id).last()
            if first_obj and last_obj:
                status = True
                chart_result = "Pass" if first_obj.recv_chart == last_obj.recv_chart else "Fail"
                tts_result = "Pass" if first_obj.recv_tts == last_obj.recv_tts else "Fail"
                sql_result = "Pass" if first_obj.recv_sql == last_obj.recv_sql else "Fail"
                data_result = "Pass" if first_obj.recv_data == last_obj.recv_data else "Fail"
            else:
                status = False
                chart_result = tts_result = sql_result = data_result = None
            models.BaseData.objects.filter(pk=id).update(data_result=data_result, sql_result=sql_result,
                                                         result=sql_result, )
            content = {
                "status": True,
            }
        return JsonResponse(content)


def get_comparedata(request):
    if request.method == "POST":
        basedata_id = request.POST.get('basedata_id')
        obj = models.BaseData.objects.filter(id=basedata_id).first()
        compare_data = models.CompareData.objects.filter(cmd=obj)

        json_data = serializers.serialize('json',compare_data)
        content = {
            "compare_data": json_data
        }

        return JsonResponse(content,safe=False)
def people_check(request):
    if request.method == 'POST':
        basedata_id = request.POST.get('basedata_id')
        peopelComment = request.POST.get('peopelComment')
        peoCheckResult = request.POST.get('peoCheckResult')
        models.BaseData.objects.filter(pk=basedata_id).update(manual_result=peoCheckResult,comment=peopelComment)
        content = {
            "status": True
        }

        return JsonResponse(content)
